package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.*;
import com.monitor.entity.*;
import com.monitor.service.ErpInspectionOrderService;
import com.monitor.service.ErpTireArchivesService;
import com.monitor.service.ErpTireSensorService;
import com.monitor.util.DataUtil;
import com.monitor.util.GetDate;
import com.monitor.util.IDUtils;
import com.monitor.util.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 轮胎传感器对应表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-27
 */
@Service
public class ErpTireSensorServiceImpl extends ServiceImpl<ErpTireSensorMapper, ErpTireSensor> implements ErpTireSensorService {
	@Resource
	private ErpCustomerCarMapper erpCustomerCarMapper;
	
	@Resource
	private ErpCarTireArchivesMapper erpCarTireArchivesMapper;
	
	@Resource
	private ErpMaterielMapper erpMaterielMapper;
	
	@Resource
	private ErpTireSensorMapper erpTireSensorMapper;
	
	@Resource
	private ErpCustomerMapper erpCustomerMapper;
	
	@Resource
	private ErpCustomerWarehouseMapper erpCustomerWarehouseMapper;
	
	@Resource
	private ErpUserMapper erpUserMapper;
	
	@Resource
	private ErpSheetIdMapper erpSheetIdMapper;
	
	@Resource
	private ErpWarehouseMapper erpWarehouseMapper;
	
	@Resource
	private ErpMaterielFlowSummaryMapper erpMaterielFlowSummaryMapper;
	
	@Resource
	private ErpParametersMapper erpParametersMapper;
	
	@Resource
	private ErpTireArchivesMapper erpTireArchivesMapper;
	
	@Resource
	private ErpTireArchivesWorkMapper erpTireArchivesWorkMapper;
	
	@Resource
	private ErpInspectionContentMapper erpInspectionContentMapper;

    @Resource
    private ErpInspectionOrderService erpInspectionOrderService;
    
    @Resource
    private ErpTireArchivesService erpTireArchivesService;
    
    @Resource
    private ErpBidataMapper erpBidataMapper;
    
    @Resource
    private ErpBidataListMapper erpBidataListMapper;
    
    @Resource
    private ErpSystemParametersChildMapper erpSystemParametersChildMapper;
    
	@Override
	public Result<List<OpeningCarTire>> findCarDetailed(String carPlate) {
		// TODO Auto-generated method stub
		ErpCustomerCar cusCar =erpCustomerCarMapper.selectByPlate(carPlate);
		if(cusCar==null) {
			return new Result<>(1, "该车没有物料信息");
		}
		List<ErpCarTireArchives> ctalist =erpCarTireArchivesMapper.selectList(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCar.getId()));
		if(ctalist.size()==0) {
			return new Result<>(2, "该车没有安装记录");
		}
		List<OpeningCarTire> octlist = new ArrayList<OpeningCarTire>();
		for(ErpCarTireArchives cta:ctalist){
			//获取轮胎详细信息
			ErpMateriel materiel =  erpMaterielMapper.selectById(cta.getTireId());


			if(materiel!=null) {
				OpeningCarTire oct = new OpeningCarTire();
				oct.setTireStorageDate(cta.getTirePurchaseDate());
				oct.setTireLocation(cta.getTireLocation());
				oct.setTireName(materiel.getMaterielName());
				oct.setTireNum(materiel.getMaterielNumber());
				oct.setTireMileage(cta.getTirePurchaseMileage());
				oct.setTireBrand(materiel.getMaterielBrand());
				oct.setTireSpecifications(materiel.getMaterielSpecifications());
				oct.setTireModel(materiel.getMaterielModel());
				oct.setTirePlace(materiel.getMaterielPlace());
				oct.setTireState(materiel.getMaterielState());
				oct.setTireUnit(materiel.getMaterielUnit());
				oct.setTireProductionId(materiel.getMaterielProductionId());
				oct.setTireStoragePrice(cta.getTirePurchasePrice());
				oct.setZhiNeng(materiel.getZhiNeng());
				oct.setQrCode(materiel.getMaterielEqcode());
				oct.setRfId(materiel.getMaterielRfid());
			
				//查询轮胎的传感器
				List<ErpTireSensor> tireSensors = erpTireSensorMapper.selectList(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", cta.getId()));
				if(tireSensors.size() > 0){
					ErpTireSensor ts = tireSensors.get(0);
					//查询传感器信息
					ErpMateriel materielSensor = erpMaterielMapper.selectById(ts.getSensorId());
					if(materielSensor!=null) {
						oct.setSensorName(materielSensor.getMaterielName());
						oct.setSensorNum(materielSensor.getMaterielNumber());
						oct.setSensorBrand(materielSensor.getMaterielBrand());
						oct.setSensorSpecifications(materielSensor.getMaterielSpecifications());
						oct.setSensorModel(materielSensor.getMaterielModel());
						oct.setSensorPlace(materielSensor.getMaterielPlace());
						oct.setSensorState(materielSensor.getMaterielState());
						oct.setSensorUnit(materielSensor.getMaterielUnit());
						oct.setSensorProductionId(materielSensor.getMaterielProductionId());
						oct.setSensorStoragePrice(cta.getSensorsPurchasePrice());
						oct.setSensorQrCode(materielSensor.getMaterielEqcode());
						oct.setSensorRfId(materielSensor.getMaterielRfid());
					}
					ErpMateriel rimId =  erpMaterielMapper.selectById(ts.getRimsId());
					if(rimId!=null) {
						oct.setRimsName(rimId.getMaterielName());
						oct.setRimsNum(rimId.getMaterielNumber());
						oct.setRimsBrand(rimId.getMaterielBrand());
						oct.setRimsSpecifications(rimId.getMaterielSpecifications());
						oct.setRimsModel(rimId.getMaterielModel());
						oct.setRimsPlace(rimId.getMaterielPlace());
						oct.setRimsState(rimId.getMaterielState());
						oct.setRimsUnit(rimId.getMaterielUnit());
						oct.setRimsProductionId(rimId.getMaterielProductionId());
						oct.setRimsStoragePrice(cta.getSensorsPurchasePrice());
						oct.setRimsRfId(rimId.getMaterielRfid());
						oct.setRimsQrCode(rimId.getMaterielEqcode());
					}
				}
				
				
				octlist.add(oct);
			}
		}
		return new Result<List<OpeningCarTire>>(octlist, octlist.size());
	}


	@Override
	public Result<Integer> addOpeningCarTire(OpeningCarTire op,int custId,int userId) {
		// TODO Auto-generated method stub
		//ErpMaterielCopy copy=new ErpMaterielCopy();
		ErpCustomer cus =  erpCustomerMapper.selectById(custId);
		ErpMateriel mTire = erpMaterielMapper.selectByProductionIdAndBrand(op.getTireProductionId(), op.getTireBrand(), op.getTireSpecifications());
		ErpCarTireArchives cta=null;
		int customerId = -1;
		if(mTire != null){
			customerId =erpCustomerWarehouseMapper.selectById(mTire.getWarehouseId()).getCustomerId();
			//return new Result<>(4,"生产编号重复");
		}
		
		//判断轮胎是否已经上车
		if(mTire != null && mTire.getMaterielLoad().equals(1)){
			//轮胎上车
			return new Result<>(1,"轮胎上车");
		}else if(mTire != null && customerId != cus.getId()){
			return new Result<>(3,"不属于该客户");
		}else{
			//轮胎未上车
			int mSensorNull = 0;
			ErpMateriel mSensor = new ErpMateriel();
			//判断是否有传感器  然后判断传感器是否上车
			if(StringUtils.isNotBlank(op.getSensorProductionId())){
				mSensor = erpMaterielMapper.selectByProductionIdAndCustId(op.getSensorProductionId(), cus.getId());
				if(mSensor != null && mSensor.getMaterielLoad().equals(1)){
					mSensorNull = 1;
				}
			}
			//有此传感器 并且已经上车
			if(mSensorNull == 1){
				return new Result<>(2,"有此传感器 并且已经上车");
			}else{
				ErpWarehouse tireW = new ErpWarehouse();
				ErpWarehouse sensorW = new ErpWarehouse();
				ErpUser user = erpUserMapper.selectById(userId);
				GetDate gd = new GetDate();
				
				ErpCustomerCar c =erpCustomerCarMapper.selectByPlate(op.getCarPlate());
				List<ErpCustomerWarehouse> cwlist = erpCustomerWarehouseMapper.selectList(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id", custId));
				ErpCustomerWarehouse cw = null;
				if(cwlist.size() == 0){
					cw = new ErpCustomerWarehouse();
					cw.setCustomerId(cus.getId());
					cw.setWarName(cus.getCusName()+"1号仓库");
					cw.setWarAddress(cus.getCusAddress());
					cw.setWarHead(user.getRealName());
					cw.setWarPhone("");
					cw.setWarInput(user.getRealName());
					cw.setWarDate((new Date()).toString());
					erpCustomerWarehouseMapper.insert(cw);
				}else{
					cw = cwlist.get(0);
				}
				
				ErpSheetId si = new ErpSheetId();
				si.setSheetDate(gd.getdate4(op.getTireStorageDate(), "yyyy-MM-dd"));
				si.setSheetType("CLWL");
				si.setSheetSort(1);
				List<ErpSheetId> silist = erpSheetIdMapper.selectList(new EntityWrapper<ErpSheetId>().eq("sheet_date", gd.getdate4(op.getTireStorageDate(), "yyyy-MM-dd")).eq("sheet_type", "CLWL").orderBy("id", false));
				String workSheetId = "CLWL" + gd.getDate0(op.getTireStorageDate(), "yyyy-MM-dd", "yyyyMMdd") + "00001";
				if(silist.size() > 0){
					int SheetSort = silist.get(0).getSheetSort() + 1;
					si.setSheetSort(SheetSort);
					if(SheetSort >= 10){
						workSheetId = "CLWL" + gd.getDate0(op.getTireStorageDate(), "yyyy-MM-dd", "yyyyMMdd") +"000"+ SheetSort;
					}else{
						workSheetId = "CLWL" + gd.getDate0(op.getTireStorageDate(), "yyyy-MM-dd", "yyyyMMdd") + "0000" + SheetSort;
					}
				}
				si.setSheetId(workSheetId);
				si.setSheetComplete(1);
				erpSheetIdMapper.insert(si);
				
				String sheetId="CLWL"+IDUtils.genOrderId();    //加上00001
				ErpMaterielFlowSummary emfs=erpMaterielFlowSummaryMapper.selectBySheelId("'%"+sheetId+"%'");
				if(emfs==null) {
					sheetId=sheetId+"00001";
				}else {
					String sheelIds=emfs.getWorkSheetId().substring(12,emfs.getWorkSheetId().length());
					sheelIds=IDUtils.addOne(sheelIds);
					sheetId=sheetId+sheelIds;
				}
				
				//先做传感器的上车处理 因为有传感器和无传感器轮胎的性质不一样
				//判断是否有传感器
				if(op.getZhiNeng()){
					if(mSensor == null){
						ErpMateriel m = new ErpMateriel();
						m.setWarehouseId(cw.getId());
						m.setMaterielName(op.getSensorName());
						m.setMaterielBrand(op.getSensorBrand());
						m.setMaterielSpecifications(op.getSensorSpecifications());
						m.setMaterielModel(op.getSensorModel());
						m.setMaterielPlace(op.getSensorPlace());
						m.setMaterielState(op.getSensorState());
						m.setMaterielUnit(op.getSensorUnit());
						m.setMaterielProductionId(op.getSensorProductionId());
						m.setMaterielDot("");
						m.setMaterielStorageDate(op.getTireStorageDate());
						m.setMaterielStoragePrice(op.getSensorStoragePrice());
						m.setMaterielLoad(1);
						m.setMaterielRefurbished(0);
						m.setMaterielEqcode(op.getSensorQrCode());
						m.setMaterielRfid(op.getSensorRfId());
						m.setTai(false);
						m.setZhiNeng(false);
						m.setMaterielNumber(op.getSensorNum());
						m.setMaterielInvoiceType(0);
						m.setMaterielId(0);
						if(op.getSensorState().equals("旧物品")){
							m.setMaterielRefurbished(-1);
						}else if(op.getSensorState().indexOf("翻新") > -1){
							m.setMaterielRefurbished(Integer.decode(op.getSensorState().substring(2, 3)));
						}
						m.setDelectType(0);
						m.setQichutype("其他");
						m.setCustomerId(custId);
						m.setKucunorliucun("库存");
						//copy=BlankUtil.copyMateriel(m);
						//erpMaterielCopyMapper.insert(copy);
						erpMaterielMapper.insert(m);
						mSensor = m;
						
						ErpWarehouse ware=new ErpWarehouse();
						ware.setWarehouseId(cw.getId());
						ware.setCustomerId(cus.getId());
						ware.setMaterielName(op.getSensorName());
						ware.setMaterielBrand(op.getSensorBrand());
						ware.setMaterielSpecifications(op.getSensorSpecifications());
						ware.setMaterielModel(op.getSensorModel());
						ware.setMaterielPlace(op.getSensorPlace());
						ware.setMaterielState(op.getSensorState());
						ware.setMaterielUnit(op.getSensorUnit());
						sensorW = erpWarehouseMapper.selectWareHouse(ware);
						if(sensorW == null){
							sensorW = new ErpWarehouse();
							sensorW.setWarehouseId(cw.getId());
							sensorW.setCustomerId(cus.getId());
							sensorW.setMaterielName(op.getSensorName());
							sensorW.setMaterielBrand(op.getSensorBrand());
							sensorW.setMaterielSpecifications(op.getSensorSpecifications());
							sensorW.setMaterielModel(op.getSensorModel());
							sensorW.setMaterielPlace(op.getSensorPlace());
							sensorW.setMaterielState(op.getSensorState());
							sensorW.setMaterielUnit(op.getSensorUnit());
							sensorW.setMaterielNumber(0);
							sensorW.setMaterielOccupyNumber(0);
							sensorW.setMaterielWarnAmount(0);
							sensorW.setMaterielTax(op.getSensorStoragePrice());
							sensorW.setMaterielNotax(op.getSensorStoragePrice());
							sensorW.setMaterielId(m.getId());
							erpWarehouseMapper.insert(sensorW); 
						}
						ErpMaterielFlowSummary mfs = new ErpMaterielFlowSummary();
						mfs.setMaterielId(sensorW.getId());
						mfs.setWarehouseId(cw.getId());
						mfs.setWorkSheetId(sheetId);
						mfs.setMaterielFlowDate(op.getTireStorageDate());
						mfs.setMaterielStorageNumber(1);
						mfs.setMaterielStoragePrice(op.getSensorStoragePrice());
						mfs.setMaterielOutflowNumber(0);
						mfs.setMaterielOutflowPrice(0.0);
						mfs.setIncludingTax(0);
						mfs.setMaterielFlowType(0);
						mfs.setFlowSubject("车辆物料");
						mfs.setMaterielSource("--");
						mfs.setMaterielWhereabouts(cw.getWarName());
						mfs.setFlowAttn(user.getRealName());
						erpMaterielFlowSummaryMapper.insert(mfs);
						
						mfs = new ErpMaterielFlowSummary();
						mfs.setMaterielId(sensorW.getId());
						mfs.setWarehouseId(cw.getId());
						mfs.setWorkSheetId(sheetId);
						mfs.setMaterielFlowDate(op.getTireStorageDate());
						mfs.setMaterielStorageNumber(0);
						mfs.setMaterielStoragePrice(0.0);
						mfs.setMaterielOutflowNumber(1);
						mfs.setMaterielOutflowPrice(op.getSensorStoragePrice());
						mfs.setIncludingTax(0);
						mfs.setMaterielFlowType(1);
						mfs.setFlowSubject("车辆物料");
						mfs.setMaterielSource(cw.getWarName());
						mfs.setMaterielWhereabouts(op.getCarPlate()+"-"+op.getTireLocation());
						mfs.setFlowAttn(user.getRealName());
						erpMaterielFlowSummaryMapper.insert(mfs);
						
					}else{
						//仓库物料
						ErpWarehouse ware=new ErpWarehouse();
						ware.setWarehouseId(cw.getId());
						ware.setCustomerId(cus.getId());
						ware.setMaterielName(op.getSensorName());
						ware.setMaterielBrand(op.getSensorBrand());
						ware.setMaterielSpecifications(op.getSensorSpecifications());
						ware.setMaterielModel(op.getSensorModel());
						ware.setMaterielPlace(op.getSensorPlace());
						ware.setMaterielState(op.getSensorState());
						ware.setMaterielUnit(op.getSensorUnit());
						sensorW = erpWarehouseMapper.selectWareHouse(ware);
						if(sensorW == null){
							//仓库无此物料 创建物料
							sensorW = new ErpWarehouse();
							sensorW.setWarehouseId(cw.getId());
							sensorW.setCustomerId(cus.getId());
							sensorW.setMaterielName(op.getSensorName());
							sensorW.setMaterielBrand(op.getSensorBrand());
							sensorW.setMaterielSpecifications(op.getSensorSpecifications());
							sensorW.setMaterielModel(op.getSensorModel());
							sensorW.setMaterielPlace(op.getSensorPlace());
							sensorW.setMaterielState(op.getSensorState());
							sensorW.setMaterielUnit(op.getSensorUnit());
							sensorW.setMaterielNumber(0);
							sensorW.setMaterielOccupyNumber(0);
							sensorW.setMaterielWarnAmount(0);
							sensorW.setMaterielTax(op.getSensorStoragePrice());
							sensorW.setMaterielNotax(op.getSensorStoragePrice());
							sensorW.setMaterielId(mSensor.getId());
							erpWarehouseMapper.insert(sensorW); 
						}
						//if(sensorW.getMaterielNumber()>0) {
							//sensorW.setMaterielNumber(sensorW.getMaterielNumber() -1);
							//erpWarehouseMapper.updateAllColumnById(sensorW);
						//}else {
							//创建入库表
							ErpMaterielFlowSummary mfs1 = new ErpMaterielFlowSummary();
							mfs1.setMaterielId(sensorW.getId());
							mfs1.setWarehouseId(cw.getId());
							mfs1.setWorkSheetId(sheetId);
							mfs1.setMaterielFlowDate(op.getTireStorageDate());
							mfs1.setMaterielStorageNumber(1);
							mfs1.setMaterielStoragePrice(op.getSensorStoragePrice());
							mfs1.setMaterielOutflowNumber(0);
							mfs1.setMaterielOutflowPrice(0.0);
							mfs1.setIncludingTax(0);
							mfs1.setMaterielFlowType(0);
							mfs1.setFlowSubject("车辆物料");
							mfs1.setMaterielSource("--");
							mfs1.setMaterielWhereabouts(cw.getWarName());
							mfs1.setFlowAttn(user.getRealName());
							erpMaterielFlowSummaryMapper.insert(mfs1);
						//}
						//创建出库表
						ErpMaterielFlowSummary mfs = new ErpMaterielFlowSummary();
						mfs.setMaterielId(sensorW.getId());
						mfs.setWarehouseId(cw.getId());
						mfs.setWorkSheetId(sheetId);
						mfs.setMaterielFlowDate(op.getTireStorageDate());
						mfs.setMaterielStorageNumber(0);
						mfs.setMaterielStoragePrice(0.0);
						mfs.setMaterielOutflowNumber(1);
						mfs.setMaterielOutflowPrice(op.getSensorStoragePrice());
						mfs.setIncludingTax(0);
						mfs.setMaterielFlowType(1);
						mfs.setFlowSubject("车辆物料");
						mfs.setMaterielSource(cw.getWarName());
						mfs.setMaterielWhereabouts(op.getCarPlate()+"-"+op.getTireLocation());
						mfs.setFlowAttn(user.getRealName());
						mfs.setOutWarehouseId(cw.getId());
						erpMaterielFlowSummaryMapper.insert(mfs);
						
						//物料上车
						mSensor.setMaterielLoad(1);
						mSensor.setMaterielFlowId(mfs.getId());
						erpMaterielMapper.updateAllColumnById(mSensor);
					}
				}
				
				//查询轮胎期初信息
				List<ErpParameters> parameters = erpParametersMapper.selectByArandAndNormsAndModelAndAddress(op.getTireBrand(), op.getTireSpecifications(), op.getTireModel(), op.getTirePlace());
				Double deepLines = 0.0;
				for(ErpParameters p:parameters){
					if(StringUtils.isNotBlank(p.getLinesS())){
						deepLines = Double.valueOf(p.getLinesS());
					}
				}
				if(mTire == null){//无此轮胎
					//创建物料
					ErpMateriel m = new ErpMateriel();
					m.setWarehouseId(cw.getId());
					m.setMaterielName(op.getTireName());
					m.setMaterielBrand(op.getTireBrand());
					m.setMaterielSpecifications(op.getTireSpecifications());
					m.setMaterielModel(op.getTireModel());
					m.setMaterielPlace(op.getTirePlace());
					m.setMaterielState(op.getTireState());
					m.setMaterielUnit(op.getTireUnit());
					m.setMaterielProductionId(op.getTireProductionId());
					m.setMaterielDot("");
					m.setMaterielStorageDate(op.getTireStorageDate());
					m.setMaterielStoragePrice(op.getTireStoragePrice());
					m.setMaterielLoad(1);
					m.setMaterielRefurbished(0);
					m.setTai(true);
					m.setZhiNeng(op.getZhiNeng());
					m.setMaterielEqcode(op.getQrCode());
					m.setMaterielRfid(op.getRfId());
					m.setState(2);
					m.setMaterielInvoiceType(0);
					m.setMaterielNumber(op.getTireNum());
					m.setCorrespondingId(mSensor.getId());
					m.setMaterielId(0);
					if (mSensor!=null)
						if(mSensor.getId()!=null)
					m.setCorrespondingId(mSensor.getId());
					if(op.getTireState().equals("旧物品")){
						m.setMaterielRefurbished(-1);
					}else if(op.getTireState().indexOf("翻新") > -1){
						m.setMaterielRefurbished(Integer.decode(op.getTireState().substring(2, 3)));
					}
					m.setDelectType(1);
					m.setQichutype("其他");
					m.setCustomerId(custId);
					m.setKucunorliucun("库存");
					//ErpMaterielCopy copy1=BlankUtil.copyMateriel(m);
					//copy1.setCorrespondingId(copy.getId());
					//erpMaterielCopyMapper.insert(copy1);
					erpMaterielMapper.insert(m);
					if(op.getTireLocation().equals("备胎")) {
						//ErpSystemParametersChild child=erpSystemParametersChildMapper.selectByName(m.getMaterielName());
						if(m.getMaterielName().trim().equals("主机(胎压)")) {
							ErpBidata bi=erpBidataMapper.selectByPlate(c.getCarPlate());
							if(bi==null) {
								bi=new ErpBidata();
								bi.setAddDate(DataUtil.dateToStr(new Date()));
								bi.setCarNum(c.getCarNumber());
								bi.setCarType(c.getCarType1());
								bi.setCarPlate(c.getCarPlate());
								bi.setCusId(custId);
								bi.setTirePressure(c.getCarWheel());
								bi.setUserId(userId);
								bi.setUserName(cus.getCusName());
								erpBidataMapper.insert(bi);
							}
							ErpBidataList biList=erpBidataListMapper.selectByBiIdAndName(bi.getId(), m.getMaterielName());
							if(biList==null) {
								biList=new ErpBidataList();
								biList.setBiId(bi.getId());
								biList.setParameterId(482);
								biList.setParameterName(m.getMaterielName());
								biList.setParameterType(1);
								biList.setParameterValue(m.getMaterielProductionId());
								erpBidataListMapper.insert(biList);
							}else {
								biList.setParameterValue(m.getMaterielProductionId());
								erpBidataListMapper.updateById(biList);
							}
						}
					}
					if (mSensor!=null) {
						mSensor.setCorrespondingId(m.getId());
						//copy.setCorrespondingId(copy1.getId());
						erpMaterielMapper.updateById(mSensor);
						//erpMaterielCopyMapper.updateById(copy);
					}
					mTire = m;

					ErpWarehouse ware=new ErpWarehouse();
					ware.setWarehouseId(cw.getId());
					ware.setCustomerId(cus.getId());
					ware.setMaterielName(op.getTireName());
					ware.setMaterielBrand(op.getTireBrand());
					ware.setMaterielSpecifications(op.getTireSpecifications());
					ware.setMaterielModel(op.getTireModel());
					ware.setMaterielPlace(op.getTirePlace());
					ware.setMaterielState(op.getTireState());
					ware.setMaterielUnit(op.getTireUnit());
					tireW = erpWarehouseMapper.selectWareHouse(ware);
					
					if(tireW == null){
						//仓库无此物料 创建物料
						tireW = new ErpWarehouse();
						tireW.setWarehouseId(cw.getId());
						tireW.setCustomerId(cus.getId());
						tireW.setMaterielName(op.getTireName());
						tireW.setMaterielBrand(op.getTireBrand());
						tireW.setMaterielSpecifications(op.getTireSpecifications());
						tireW.setMaterielModel(op.getTireModel());
						tireW.setMaterielPlace(op.getTirePlace());
						tireW.setMaterielState(op.getTireState());
						tireW.setMaterielUnit(op.getTireUnit());
						tireW.setMaterielNumber(0);
						tireW.setMaterielOccupyNumber(0);
						tireW.setMaterielWarnAmount(0);
						tireW.setMaterielTax(op.getTireStoragePrice());
						tireW.setMaterielNotax(op.getTireStoragePrice());
						tireW.setMaterielId(m.getId());
						erpWarehouseMapper.insert(tireW); 
					}
					//创建出入库表
					ErpMaterielFlowSummary mfs = new ErpMaterielFlowSummary();
					mfs.setWarehouseId(cw.getId());
					mfs.setMaterielId(tireW.getId());
					mfs.setWorkSheetId(sheetId);
					mfs.setMaterielFlowDate(op.getTireStorageDate());
					mfs.setMaterielStorageNumber(1);
					mfs.setMaterielStoragePrice(op.getTireStoragePrice());
					mfs.setMaterielOutflowNumber(0);
					mfs.setMaterielOutflowPrice(0.0);
					mfs.setIncludingTax(0);
					mfs.setMaterielFlowType(0);
					mfs.setFlowSubject("车辆物料");
					mfs.setMaterielSource("--");
					mfs.setMaterielWhereabouts(cw.getWarName());
					mfs.setFlowAttn(user.getRealName());
					erpMaterielFlowSummaryMapper.insert(mfs);
					
					mfs = new ErpMaterielFlowSummary();
					mfs.setWarehouseId(cw.getId());
					mfs.setMaterielId(tireW.getId());
					mfs.setWorkSheetId(sheetId);
					mfs.setMaterielFlowDate(op.getTireStorageDate());
					mfs.setMaterielStorageNumber(0);
					mfs.setMaterielStoragePrice(0.0);
					mfs.setMaterielOutflowNumber(1);
					mfs.setMaterielOutflowPrice(op.getTireStoragePrice());
					mfs.setIncludingTax(0);
					mfs.setMaterielFlowType(1);
					mfs.setFlowSubject("车辆物料");
					mfs.setMaterielSource(cw.getWarName());
					mfs.setMaterielWhereabouts(op.getCarPlate()+"-"+op.getTireLocation());
					mfs.setFlowAttn(user.getRealName());
					mfs.setOutWarehouseId(cw.getId());
					erpMaterielFlowSummaryMapper.insert(mfs);
					 //创建轮胎档案
					
					ErpTireArchives ta = new ErpTireArchives();
					ta.setTireId(m.getId());
					ta.setTireTread1(deepLines);
					ta.setTireTread2(deepLines);
					ta.setTireTread3(deepLines);
					ta.setTireTread4(deepLines);
					erpTireArchivesMapper.insert(ta);
					
					//创建轮胎档案工作
					ErpTireArchivesWork taw = new ErpTireArchivesWork();
					taw.setTireId(m.getId());
					taw.setWorkDate(gd.getDate2(op.getTireStorageDate(), "yyyy-MM-dd"));
					taw.setWorkSheetId(workSheetId);
					taw.setWorkServiceSector(cus.getCusName());
					taw.setTireLocation(op.getCarPlate()+"-"+op.getTireLocation());
					taw.setWorkMileage(op.getTireMileage());
					taw.setWorkProject("车辆物料");
					taw.setWorkTireState(op.getTireState());
					taw.setTireCost(op.getTireStoragePrice());
					taw.setTireState(op.getTireState());
					taw.setCustomerId(cus.getId());
					taw.setCostType(0);
					taw.setCalculatingMileage(0);
					erpTireArchivesWorkMapper.insert(taw);
					
					//创建车辆轮胎档案
					cta = new ErpCarTireArchives();
					cta.setTireId(m.getId());
					cta.setCarId(c.getId());
					cta.setTireLocation(op.getTireLocation());
					cta.setTirePurchaseDate(op.getTireStorageDate());
					cta.setTirePurchasePrice(op.getTireStoragePrice());
					cta.setTireMaintenance(0.0);
					cta.setTireAverage(0.0);
					cta.setTirePurchaseMileage(op.getTireMileage());
					cta.setSensorsPurchasePrice(0.0);
					if(op.getSensorStoragePrice() != null){
						cta.setSensorsPurchasePrice(op.getSensorStoragePrice());
					}
					if(mSensor.getId()!=null&&mSensor.getId()!=0) {
						cta.setSensorsId(mSensor.getId());
					}else {
						cta.setSensorsId(0);
					}
					cta.setPositionerId(0);
					cta.setRimId(0);
					List<ErpCarTireArchives> list1=erpCarTireArchivesMapper.selectList(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cta.getCarId()).and().eq("tire_location", cta.getTireLocation()));
					if(list1.size()>0) {
						cta.setId(list1.get(0).getId());
						erpCarTireArchivesMapper.updateById(cta);
					}else {
						erpCarTireArchivesMapper.insert(cta);
					}
					
					this.modifyInspection(m, cta);
				}else{
					//仓库物料-1
					ErpWarehouse ware=new ErpWarehouse();
					ware.setWarehouseId(cw.getId());
					ware.setCustomerId(cus.getId());
					ware.setMaterielName(op.getTireName());
					ware.setMaterielBrand(op.getTireBrand());
					ware.setMaterielSpecifications(op.getTireSpecifications());
					ware.setMaterielModel(op.getTireModel());
					ware.setMaterielPlace(op.getTirePlace());
					ware.setMaterielState(op.getTireState());
					ware.setMaterielUnit(op.getTireUnit());
					tireW = erpWarehouseMapper.selectWareHouse(ware);
					//tireW.setMaterielNumber(tireW.getMaterielNumber() -1);
					//erpWarehouseMapper.updateAllColumnById(tireW);
				    
					//ware.setMaterielName(op.getTireName());
					//tireW = erpWarehouseMapper.selectWareHouse(ware);
			
					if(tireW == null){
						//仓库无此物料 创建物料
						tireW = new ErpWarehouse();
						tireW.setWarehouseId(cw.getId());
						tireW.setCustomerId(cus.getId());
						tireW.setMaterielName(op.getTireName());
						tireW.setMaterielBrand(op.getTireBrand());
						tireW.setMaterielSpecifications(op.getTireSpecifications());
						tireW.setMaterielModel(op.getTireModel());
						tireW.setMaterielPlace(op.getTirePlace());
						tireW.setMaterielState(op.getTireState());
						tireW.setMaterielUnit(op.getTireUnit());
						tireW.setMaterielNumber(0);
						tireW.setMaterielOccupyNumber(0);
						tireW.setMaterielWarnAmount(0);
						tireW.setMaterielTax(op.getTireStoragePrice());
						tireW.setMaterielNotax(op.getTireStoragePrice());
						erpWarehouseMapper.insert(tireW); 
					}
					ErpMaterielFlowSummary mfs = new ErpMaterielFlowSummary();
					mfs.setWarehouseId(cw.getId());
					mfs.setMaterielId(tireW.getId());
					mfs.setWorkSheetId(sheetId);
					mfs.setMaterielFlowDate(op.getTireStorageDate());
					mfs.setMaterielStorageNumber(1);
					mfs.setMaterielStoragePrice(op.getTireStoragePrice());
					mfs.setMaterielOutflowNumber(0);
					mfs.setMaterielOutflowPrice(0.0);
					mfs.setIncludingTax(0);
					mfs.setMaterielFlowType(0);
					mfs.setFlowSubject("车辆物料");
					mfs.setMaterielSource("--");
					mfs.setMaterielWhereabouts(cw.getWarName());
					mfs.setFlowAttn(user.getRealName());
					erpMaterielFlowSummaryMapper.insert(mfs);
				  //创建出库表
					mfs = new ErpMaterielFlowSummary();
					mfs.setMaterielId(tireW.getId());
					mfs.setWarehouseId(cw.getId());
					mfs.setWorkSheetId(sheetId);
					mfs.setMaterielFlowDate(op.getTireStorageDate());
					mfs.setMaterielStorageNumber(0);
					mfs.setMaterielStoragePrice(0.0);
					mfs.setMaterielOutflowNumber(1);
					mfs.setMaterielOutflowPrice(op.getTireStoragePrice());
					mfs.setIncludingTax(0);
					mfs.setMaterielFlowType(1);
					mfs.setFlowSubject("车辆物料");
					mfs.setMaterielSource(cw.getWarName());
					mfs.setMaterielWhereabouts(op.getCarPlate()+"-"+op.getTireLocation());
					mfs.setFlowAttn(user.getRealName());
					mfs.setOutWarehouseId(cw.getId());
					erpMaterielFlowSummaryMapper.insert(mfs);
				    
				    //物料上车
				    mTire.setMaterielLoad(1);
				    mTire.setMaterielName(op.getTireName());
					erpMaterielMapper.updateAllColumnById(mTire);
					
					
					//创建轮胎档案工作
					ErpTireArchivesWork taw = new ErpTireArchivesWork();
					taw.setTireId(mTire.getId());
					taw.setWorkDate(gd.getDate2(op.getTireStorageDate(), "yyyy-MM-dd"));
					taw.setWorkSheetId(workSheetId);
					taw.setWorkServiceSector(cus.getCusName());
					taw.setTireLocation(op.getCarPlate()+"-"+op.getTireLocation());
					taw.setWorkMileage(op.getTireMileage());
					taw.setWorkProject("车辆物料");
					taw.setWorkTireState(op.getTireState());
					taw.setTireCost(op.getTireStoragePrice());
					taw.setTireState(op.getTireState());
					taw.setCustomerId(cus.getId());
					taw.setCostType(0);
					taw.setCalculatingMileage(0);
					erpTireArchivesWorkMapper.insert(taw);
					
					//创建车辆轮胎档案
					cta = new ErpCarTireArchives();
					cta.setTireId(mTire.getId());
					cta.setCarId(c.getId());
					cta.setTireLocation(op.getTireLocation());
					cta.setTirePurchaseDate(op.getTireStorageDate());
					cta.setTirePurchasePrice(op.getTireStoragePrice());
					cta.setTireMaintenance(0.0);
					cta.setTireAverage(0.0);
					cta.setTirePurchaseMileage(op.getTireMileage());
					cta.setSensorsPurchasePrice(0.0);
					if(op.getSensorStoragePrice() != null){
						cta.setSensorsPurchasePrice(op.getSensorStoragePrice());
					}
					if(StringUtils.isNotBlank(op.getSensorProductionId())){
						cta.setSensorsId(mSensor.getId());
					}else{
						cta.setSensorsId(0);
					}
					cta.setPositionerId(0);
					cta.setRimId(0);
					List<ErpCarTireArchives> list1=erpCarTireArchivesMapper.selectList(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cta.getCarId()).and().eq("tire_location", cta.getTireLocation()));
					if(list1.size()>0) {
						cta.setId(list1.get(0).getId());
						erpCarTireArchivesMapper.updateById(cta);
					}else {
						erpCarTireArchivesMapper.insert(cta);
					}
					this.modifyInspection(mTire, cta);
				}
				
				//添加轮辋
				ErpMateriel rims = new ErpMateriel();
				rims.setWarehouseId(cw.getId());
				rims.setMaterielName(op.getRimsName());
				rims.setMaterielBrand(op.getRimsBrand());
				rims.setMaterielSpecifications(op.getRimsSpecifications());
				rims.setMaterielModel(op.getRimsModel());
				rims.setMaterielPlace(op.getRimsPlace());
				rims.setMaterielState(op.getRimsState());
				rims.setMaterielUnit(op.getRimsUnit());
				rims.setMaterielProductionId(op.getRimsProductionId());
				rims.setMaterielDot("");
				rims.setMaterielStorageDate(DataUtil.dateToStrLong(new Date()));
				rims.setMaterielStoragePrice(op.getRimsStoragePrice());
				rims.setMaterielLoad(1);
				rims.setMaterielRefurbished(0);
				rims.setTai(false);
				rims.setZhiNeng(false);
				rims.setMaterielEqcode(op.getQrCode());
				rims.setMaterielRfid(op.getRfId());
				rims.setMaterielInvoiceType(0);
				rims.setMaterielNumber(op.getRimsNum());
				rims.setCorrespondingId(0);
				rims.setMaterielId(0);
				rims.setState(0);
				if(op.getTireState().equals("旧物品")){
					rims.setMaterielRefurbished(-1);
				}else if(op.getTireState().indexOf("翻新") > -1){
					rims.setMaterielRefurbished(Integer.decode(op.getTireState().substring(2, 3)));
				}
				rims.setDelectType(1);
				rims.setQichutype("其他");
				rims.setCustomerId(custId);
				rims.setKucunorliucun("库存");
				//copy=BlankUtil.copyMateriel(rims);
				//erpMaterielCopyMapper.insert(copy);
				erpMaterielMapper.insert(rims);
				
				ErpWarehouse ware=new ErpWarehouse();

				ware.setWarehouseId(cw.getId());
				ware.setCustomerId(cus.getId());
				ware.setMaterielName(op.getRimsName());
				ware.setMaterielBrand(op.getRimsBrand());
				ware.setMaterielSpecifications(op.getRimsSpecifications());
				ware.setMaterielModel(op.getRimsModel());
				ware.setMaterielPlace(op.getRimsPlace());
				ware.setMaterielState(op.getRimsState());
				ware.setMaterielUnit(op.getRimsUnit());
				sensorW = erpWarehouseMapper.selectWareHouse(ware);
				if(sensorW == null){
					sensorW = new ErpWarehouse();
					sensorW.setWarehouseId(cw.getId());
					sensorW.setCustomerId(cus.getId());
					sensorW.setMaterielName(op.getRimsName());
					sensorW.setMaterielBrand(op.getRimsBrand());
					sensorW.setMaterielSpecifications(op.getRimsSpecifications());
					sensorW.setMaterielModel(op.getRimsModel());
					sensorW.setMaterielPlace(op.getRimsPlace());
					sensorW.setMaterielState(op.getRimsState());
					sensorW.setMaterielUnit(op.getRimsUnit());
					sensorW.setMaterielNumber(0);
					sensorW.setMaterielOccupyNumber(0);
					sensorW.setMaterielWarnAmount(0);
					sensorW.setMaterielTax(op.getRimsStoragePrice());
					sensorW.setMaterielNotax(op.getRimsStoragePrice());
					sensorW.setMaterielId(rims.getId());
					erpWarehouseMapper.insert(sensorW); 
				}
				//创建出入库表
				ErpMaterielFlowSummary mfs = new ErpMaterielFlowSummary();
				mfs.setMaterielId(sensorW.getId());
				mfs.setWarehouseId(cw.getId());
				mfs.setWorkSheetId(sheetId);
				mfs.setMaterielFlowDate(op.getTireStorageDate());
				mfs.setMaterielStorageNumber(0);
				mfs.setMaterielStoragePrice(0.0);
				mfs.setMaterielOutflowNumber(1);
				mfs.setMaterielOutflowPrice(op.getRimsStoragePrice());
				mfs.setIncludingTax(0);
				mfs.setMaterielFlowType(0);
				mfs.setFlowSubject("车辆物料");
				mfs.setMaterielSource(cw.getWarName());
				mfs.setMaterielWhereabouts(op.getCarPlate()+"-"+op.getTireLocation());
				mfs.setFlowAttn(user.getRealName());
				mfs.setOutWarehouseId(cw.getId());
				erpMaterielFlowSummaryMapper.insert(mfs);
				
				mfs = new ErpMaterielFlowSummary();
				mfs.setMaterielId(sensorW.getId());
				mfs.setWarehouseId(cw.getId());
				mfs.setWorkSheetId(sheetId);
				mfs.setMaterielFlowDate(op.getTireStorageDate());
				mfs.setMaterielStorageNumber(0);
				mfs.setMaterielStoragePrice(0.0);
				mfs.setMaterielOutflowNumber(1);
				mfs.setMaterielOutflowPrice(op.getRimsStoragePrice());
				mfs.setIncludingTax(0);
				mfs.setMaterielFlowType(1);
				mfs.setFlowSubject("车辆物料");
				mfs.setMaterielSource(cw.getWarName());
				mfs.setMaterielWhereabouts(op.getCarPlate()+"-"+op.getTireLocation());
				mfs.setFlowAttn(user.getRealName());
				mfs.setOutWarehouseId(cw.getId());
				erpMaterielFlowSummaryMapper.insert(mfs);
				
				rims.setMaterielFlowId(mfs.getId());
				
				//物料上车
				rims.setMaterielLoad(1);
				rims.setMaterielName(op.getRimsName());
				erpMaterielMapper.updateAllColumnById(rims);
				//有传感器并且是粘贴的
				ErpTireSensor ts = new ErpTireSensor();
				List<ErpTireSensor> tsList=erpTireSensorMapper.selectList(new EntityWrapper<ErpTireSensor>().eq("tire_id", mTire.getId()).orderBy("id", false));
				if(tsList.size()>0) {
					ts=tsList.get(0);
				}
				ts.setTireId(mTire.getId());
				
				
				ts.setWarehouseId(mTire.getWarehouseId());
				ts.setTirePrice(op.getTireStoragePrice());
				
				ts.setRimsId(rims.getId());
				ts.setRimsPrice(op.getRimsStoragePrice());
				if(op.getZhiNeng()){
					//创建轮胎传感器
					ts.setSensorId(mSensor.getId());
					ts.setSensorPrice(op.getSensorStoragePrice());
				}else {
					ts.setSensorPrice(0.0);
					ts.setSensorId(0);
				}
				if(cta!=null) {
					ts.setErpcartirearchivesid(cta.getId());
				}
				
				erpTireSensorMapper.insert(ts);
				String[] weels = c.getCarWheel().split("-");
				int rows = 0;
				for(String s : weels){
					rows += Integer.parseInt(s);
				}
				
				List<ErpInspectionContent> iclist = erpInspectionContentMapper.selectByPlate(op.getCarPlate(), 1, rows+1);
				if(iclist.size() > 0){
					for(ErpInspectionContent ic : iclist){
						if(ic.getTirePlace().equals(op.getTireLocation())){
							ic.setBrand(op.getTireBrand());
							ic.setSpecification(op.getTireSpecifications());
							ic.setSize(op.getTireModel());
							ic.setProducePlace(op.getTirePlace());
							ic.setState(op.getTireState());
							ic.setUnit(op.getTireUnit());
							ic.setTireId("");
							ic.setProduceId(op.getTireProductionId());
							if( ic.getDeepLines1()==null || Double.valueOf(ic.getDeepLines1())==0 ){
								ic.setDeepLines1(null);
							}
							if( ic.getDeepLines2()==null || Double.valueOf(ic.getDeepLines2())==0 ){
								ic.setDeepLines2(null);
							}
							if( ic.getDeepLines3()==null || Double.valueOf(ic.getDeepLines3())==0 ){
								ic.setDeepLines3(null);
							}
							if( ic.getDeepLines4()==null || Double.valueOf(ic.getDeepLines4())==0 ){
								ic.setDeepLines4(null);
							}
							erpInspectionContentMapper.updateById(ic);
						}
					}
				}else{
					List<String> weel = new ArrayList();
					for(int i = 0; i < weels.length; i++){
						String tires = weels[i];
						int in = Integer.parseInt(tires);
						if(in == 2){
							if(i == 0){
								String tire1 = "左1轮";
								String tire2 = "右1轮";
								weel.add(tire1);
								weel.add(tire2);
							}else if(i == weels.length - 1){
								int num = i + 1;
								String tire1 = "左" + num + "轮";
								String tire2 = "右" + num + "轮";
								weel.add(tire1);
								weel.add(tire2);
							}else{
								int num = i + 1;
								String tire1 = "左" + num + "轮";
								String tire2 = "右" + num + "轮";
								weel.add(tire1);
								weel.add(tire2);
							}
						}
						if(in == 4){
							int num = i + 1;
							String tire1 = "左" + num + "外轮";
							String tire2 = "左" + num + "内轮";
							String tire3 = "右" + num + "内轮";
							String tire4 = "右" + num + "外轮";
							weel.add(tire1);
							weel.add(tire2);
							weel.add(tire3);
							weel.add(tire4);
						}
					}
					weel.add("备胎");
					for(String tireLocation : weel){
						ErpInspectionContent ic = new ErpInspectionContent();
						ic.setCustomerId(c.getCarPlate());
						ic.setTirePlace(tireLocation);
						if(tireLocation.equals(op.getTireLocation())){
							ic.setBrand(op.getTireBrand());
							ic.setSpecification(op.getTireSpecifications());
							ic.setSize(op.getTireModel());
							ic.setProducePlace(op.getTirePlace());
							ic.setState(op.getTireState());
							ic.setUnit(op.getTireUnit());
							ic.setTireId("");
							ic.setProduceId(op.getTireProductionId());
						}
						//iclist.add(ic);
						erpInspectionContentMapper.insert(ic);
					}
					//dao.saveOrUpdateAll(iclist);
				}
				return new Result<Integer>(0, 1);
			}
		}
	}

	@Override
	public Result<List<OpeningCarTire>> getTireByCarId(String carPlate) {

			// TODO Auto-generated method stub
			ErpCustomerCar cusCar =erpCustomerCarMapper.selectByPlate(carPlate);

			if(cusCar==null) {
				return new Result<>(1, "该车没有物料信息");
			}
			List<ErpCarTireArchives> ctalist =erpCarTireArchivesMapper.selectList(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCar.getId()));
			if(ctalist.size()==0) {
				return new Result<>(2, "该车没有安装记录");
			}
			List<OpeningCarTire> octlist = new ArrayList<OpeningCarTire>();
			//GetDate gd = new GetDate();

//        erp_inspection_order

			for(ErpCarTireArchives cta:ctalist){
				//获取轮胎详细信息
				ErpMateriel materiel =  erpMaterielMapper.selectById(cta.getTireId());

					//车牌号    carPlate
				//  胎位
                ErpTireArchives tire_id = erpTireArchivesService.selectOne(new EntityWrapper<ErpTireArchives>().eq("tire_id", cta.getTireId()));
//                ErpInspectionContent erpInspectionContent=new ErpInspectionContent();
//				erpInspectionContent.setCustomerId(carPlate);
//				erpInspectionContent.setTirePlace(cta.getTireLocation());
                ErpInspectionContent  erpInspectionContent1=null;
                List<ErpInspectionContent> erpInspectionContents = erpInspectionContentMapper.selectList(new EntityWrapper<ErpInspectionContent>().eq("customer_id", carPlate).eq("tire_place", cta.getTireLocation()));
            if (erpInspectionContents.size()!=0){
                        erpInspectionContent1=erpInspectionContents.get(erpInspectionContents.size()-1);
            }
                ;//(new EntityWrapper<ErpInspectionContent>().eq("customer_id","0"));
                ErpInspectionOrder erpInspectionOrder =null;

                int count =0;
                if (erpInspectionContent1==null){

                  count=1;
                }else{
                    erpInspectionOrder = erpInspectionOrderService.selectById(erpInspectionContent1.getPid());

                }
                if(materiel!=null) {
					OpeningCarTire oct = new OpeningCarTire();
					oct.setMileage1(0.0);
					if (erpInspectionContent1==null){
//                        private Double tireDeepLines1;//轮辋内外平均纹深
//                        private Double tireDeepLines2;//轮辋内外平均纹深
//                        private Double tireDeepLines3;//轮辋内外平均纹深
//                        private Double tireDeepLines4;//轮辋内外平均纹深
//                        private  Double oldTireDeepLines1;	//轮胎内外平均纹深上期
//                        private  Double oldTireDeepLines2;//轮胎内外平均纹深
//                        private  Double oldTireDeepLines3;//轮胎内外平均纹深
//                        private  Double oldTireDeepLines4;//轮胎内外平均纹深
//                        private String tireminorRepairs; //轮胎小修
//                        private String tireId; //胎位,
//                        private String tireErrorOrTure; //报错
//                        private Double tirePressure; //胎压
//                        private String tireCoreCap; //芯帽
                        oct.setTirePressure(0.0);
                        oct.setOldTireDeepLines1(0.0);
                        oct.setOldTireDeepLines2(0.0);
                        oct.setOldTireDeepLines3(0.0);
                        oct.setOldTireDeepLines4(0.0);
                        oct.setMileage2(0.0);
                        oct.setTireStatus("");
                    }else{
					    oct.setTireStatus(erpInspectionContent1.getTireStatus());
                        oct.setTirePressure(erpInspectionContent1.getTirePressure());
                        oct.setOldTireDeepLines1(erpInspectionContent1.getDeepLines1());
                        oct.setOldTireDeepLines2(erpInspectionContent1.getDeepLines2());
                        oct.setOldTireDeepLines3(erpInspectionContent1.getDeepLines3());
                        oct.setOldTireDeepLines4(erpInspectionContent1.getDeepLines4());

                    }
//                    //统计管理,车辆物料
//                    private  Double mileage1 ;//运营里程1
//                    private Double yujilicheng1;//预计里程1
//                    private  Double tireTread1;	//轮胎胎纹深度  2 3 4
//                    private  String tiretime;//轮胎检测日期
//                            tireHu; 维修维护
                    if (erpInspectionOrder!=null){
                        oct.setMileage1(erpInspectionOrder.getMileage());
                        oct.setYujilicheng1(0.0);
                        oct.setTiretime(erpInspectionOrder.getInspectionDt());
                        oct.setTireHu(erpInspectionContentMapper.selectCount(new EntityWrapper<ErpInspectionContent>().eq("customer_id",carPlate).eq("tire_place",cta.getTireLocation())));
                    }else{
                        oct.setMileage2(0.0);
                        oct.setYujilicheng1(0.0);
                        oct.setTiretime("");
                        oct.setTireHu(0);
                    }

                    if (tire_id!=null){
                        oct.setTireTread1(tire_id.getTireTread1());
                        oct.setTireTread2(tire_id.getTireTread2());
                        oct.setTireTread3(tire_id.getTireTread3());
                        oct.setTireTread4(tire_id.getTireTread4());
                    }else {
                        oct.setTireTread1(0.0);
                        oct.setTireTread2(0.0);
                        oct.setTireTread3(0.0);
                        oct.setTireTread4(0.0);
                    }
					oct.setTireStorageDate(cta.getTirePurchaseDate());
					oct.setTireLocation(cta.getTireLocation());
					oct.setTireName(materiel.getMaterielName());
					oct.setTireNum(materiel.getMaterielNumber());
					oct.setTireMileage(cta.getTirePurchaseMileage());
					oct.setTireBrand(materiel.getMaterielBrand());
					oct.setTireSpecifications(materiel.getMaterielSpecifications());
					oct.setTireModel(materiel.getMaterielModel());
					oct.setTirePlace(materiel.getMaterielPlace());
					oct.setTireState(materiel.getMaterielState());
					oct.setTireUnit(materiel.getMaterielUnit());
					oct.setTireProductionId(materiel.getMaterielProductionId());
					oct.setTireStoragePrice(cta.getTirePurchasePrice());
					oct.setZhiNeng(materiel.getZhiNeng());
					oct.setQrCode(materiel.getMaterielEqcode());
					oct.setRfId(materiel.getMaterielRfid());

					//查询轮胎的传感器
					List<ErpTireSensor> tireSensors = erpTireSensorMapper.selectList(new EntityWrapper<ErpTireSensor>().eq("tire_id", cta.getTireId()).orderBy("id", false));
					if(tireSensors.size() > 0){
						ErpTireSensor ts = tireSensors.get(0);
						//查询传感器信息
						int sersorId=0;
						if(ts.getSensorId()!=null) {
							if(ts.getSensorId()==0) {
								sersorId=ts.getSensorId1();
							}else {
								sersorId=ts.getSensorId();
							}
						}
						ErpMateriel materielSensor = erpMaterielMapper.selectById(sersorId);
						if(materielSensor!=null) {

                            if (erpInspectionContent1==null){
//                        private Double tireDeepLines1;//轮辋内外平均纹深
//                        private Double tireDeepLines2;//轮辋内外平均纹深
//                        private Double tireDeepLines3;//轮辋内外平均纹深
//                        private Double tireDeepLines4;//轮辋内外平均纹深
//                        private  Double oldTireDeepLines1;	//轮胎内外平均纹深上期
//                        private  Double oldTireDeepLines2;//轮胎内外平均纹深
//                        private  Double oldTireDeepLines3;//轮胎内外平均纹深
//                        private  Double oldTireDeepLines4;//轮胎内外平均纹深
//                        private String tireminorRepairs; //轮胎小修
//                        private String tireId; //胎位,
//                        private String tireErrorOrTure; //报错
//                        private Double tirePressure; //胎压
//                        private String tireCoreCap; //芯帽
                                oct.setSensorPressure(0.0);
                                oct.setOldSensorDeepLines1(0.0);
                                oct.setOldSensorDeepLines2(0.0);
                                oct.setOldSensorDeepLines3(0.0);
                                oct.setOldSensorDeepLines4(0.0);
                                oct.setTireStatus("");

                            }else{
                                oct.setSensorStatus(erpInspectionContent1.getTireStatus());
                                oct.setSensorPressure(erpInspectionContent1.getTirePressure());
                                oct.setOldSensorDeepLines1(erpInspectionContent1.getDeepLines1());
                                oct.setOldSensorDeepLines2(erpInspectionContent1.getDeepLines2());
                                oct.setOldSensorDeepLines3(erpInspectionContent1.getDeepLines3());
                                oct.setOldSensorDeepLines4(erpInspectionContent1.getDeepLines4());

                            }
                            if (erpInspectionOrder!=null){
                                oct.setMileage2(erpInspectionOrder.getMileage());
                                oct.setYujilicheng2(0.0);
                                oct.setSensortime(erpInspectionOrder.getInspectionDt());
                                oct.setSensorHu(erpInspectionContentMapper.selectCount(new EntityWrapper<ErpInspectionContent>().eq("customer_id",carPlate).eq("tire_place",cta.getTireLocation())));
                            }else{
                                oct.setYujilicheng2(0.0);
                                oct.setMileage1(0.0);
                                oct.setSensortime("");
                                oct.setSensorHu(0);
                            }

                            if (tire_id!=null){
                                oct.setSensorTread1(tire_id.getTireTread1());
                                oct.setSensorTread2(tire_id.getTireTread2());
                                oct.setSensorTread3(tire_id.getTireTread3());
                                oct.setSensorTread4(tire_id.getTireTread4());
                            }else {
                                oct.setSensorTread1(0.0);
                                oct.setSensorTread2(0.0);
                                oct.setSensorTread3(0.0);
                                oct.setSensorTread4(0.0);
                            }


							oct.setSensorName(materielSensor.getMaterielName());
							oct.setSensorNum(materielSensor.getMaterielNumber());
							oct.setSensorBrand(materielSensor.getMaterielBrand());
							oct.setSensorSpecifications(materielSensor.getMaterielSpecifications());
							oct.setSensorModel(materielSensor.getMaterielModel());
							oct.setSensorPlace(materielSensor.getMaterielPlace());
							oct.setSensorState(materielSensor.getMaterielState());
							oct.setSensorUnit(materielSensor.getMaterielUnit());
							oct.setSensorProductionId(materielSensor.getMaterielProductionId());
							oct.setSensorStoragePrice(cta.getSensorsPurchasePrice());
							oct.setSensorQrCode(materielSensor.getMaterielEqcode());
							oct.setSensorRfId(materielSensor.getMaterielRfid());
						}
						ErpMateriel rimId =  erpMaterielMapper.selectById(ts.getRimsId());
						if(rimId!=null) {
                            if (erpInspectionContent1==null){
//                        private Double tireDeepLines1;//轮辋内外平均纹深
//                        private Double tireDeepLines2;//轮辋内外平均纹深
//                        private Double tireDeepLines3;//轮辋内外平均纹深
//                        private Double tireDeepLines4;//轮辋内外平均纹深
//                        private  Double oldTireDeepLines1;	//轮胎内外平均纹深上期
//                        private  Double oldTireDeepLines2;//轮胎内外平均纹深
//                        private  Double oldTireDeepLines3;//轮胎内外平均纹深
//                        private  Double oldTireDeepLines4;//轮胎内外平均纹深
//                        private String tireminorRepairs; //轮胎小修
//                        private String tireId; //胎位,
//                        private String tireErrorOrTure; //报错
//                        private Double tirePressure; //胎压
//                        private String tireCoreCap; //芯帽
                                oct.setRimsPressure(0.0);
                                oct.setOldRimsDeepLines1(0.0);
                                oct.setOldRimsDeepLines2(0.0);
                                oct.setOldRimsDeepLines3(0.0);
                                oct.setOldRimsDeepLines4(0.0);
                                oct.setMileage3(0.0);
                                oct.setTireStatus("");

                            }else{
                                oct.setRimsStatus(erpInspectionContent1.getTireStatus());
                                oct.setRimsPressure(erpInspectionContent1.getTirePressure());
                                oct.setOldRimsDeepLines1(erpInspectionContent1.getDeepLines1());
                                oct.setOldRimsDeepLines2(erpInspectionContent1.getDeepLines2());
                                oct.setOldRimsDeepLines3(erpInspectionContent1.getDeepLines3());
                                oct.setOldRimsDeepLines4(erpInspectionContent1.getDeepLines4());

                            }
                            if (erpInspectionOrder!=null){
                                oct.setMileage3(erpInspectionOrder.getMileage());
                                oct.setYujilicheng3(0.0);
                                oct.setRimstime(erpInspectionOrder.getInspectionDt());
                                oct.setRimsHu(erpInspectionContentMapper.selectCount(new EntityWrapper<ErpInspectionContent>().eq("customer_id",carPlate).eq("tire_place",cta.getTireLocation())));
                            }else{
                                oct.setMileage3(0.0);
                                oct.setYujilicheng3(0.0);
                                oct.setRimstime("");
                                oct.setRimsHu(0);
                            }

                            if (tire_id!=null){
                                oct.setRimsTread1(tire_id.getTireTread1());
                                oct.setRimsTread2(tire_id.getTireTread2());
                                oct.setRimsTread3(tire_id.getTireTread3());
                                oct.setRimsTread4(tire_id.getTireTread4());
                            }else {
                                oct.setRimsTread1(0.0);
                                oct.setRimsTread2(0.0);
                                oct.setRimsTread3(0.0);
                                oct.setRimsTread4(0.0);
                            }
							oct.setMileage3(0.0);
							oct.setRimsName(rimId.getMaterielName());
							oct.setRimsNum(rimId.getMaterielNumber());
							oct.setRimsBrand(rimId.getMaterielBrand());
							oct.setRimsSpecifications(rimId.getMaterielSpecifications());
							oct.setRimsModel(rimId.getMaterielModel());
							oct.setRimsPlace(rimId.getMaterielPlace());
							oct.setRimsState(rimId.getMaterielState());
							oct.setRimsUnit(rimId.getMaterielUnit());
							oct.setRimsProductionId(rimId.getMaterielProductionId());
							oct.setRimsStoragePrice(rimId.getMaterielStoragePrice());
							oct.setRimsRfId(rimId.getMaterielRfid());
							oct.setRimsQrCode(rimId.getMaterielEqcode());
						}
					}


					octlist.add(oct);
				}
			}
			return new Result<List<OpeningCarTire>>(octlist, octlist.size());
		}




	//修改检测维护信息表
		public boolean modifyInspection(ErpMateriel m, ErpCarTireArchives cta){
			//查询车辆牌号
			String hql = "select cc.carPlate from ErpCustomerCar cc where cc.id=?";
			String carPlate = erpCustomerCarMapper.selectById(cta.getCarId()).getCarPlate();
			List<String> TirePlaces = this.findTirePlaces(carPlate);
			//String findInspection = "from ErpInspectionContent ins where ins.customerId=? order by ins.id desc";
			List<ErpInspectionContent> inspections = erpInspectionContentMapper.selectByPlate(carPlate, 1, TirePlaces.size());
			boolean flag = true;
			if(inspections.size() < 1){
				for(int i = 0; i < TirePlaces.size(); i++){
					ErpInspectionContent newIns = new ErpInspectionContent();
					newIns.setTirePlace(TirePlaces.get(i));
					newIns.setCustomerId(carPlate);
					if(newIns.getTirePlace().equals(cta.getTireLocation())){
						newIns.setBrand(m.getMaterielBrand());
						newIns.setSpecification(m.getMaterielSpecifications());
						newIns.setSize(m.getMaterielModel());
						newIns.setProducePlace(m.getMaterielPlace());
						newIns.setState(m.getMaterielState());
						newIns.setUnit(m.getMaterielUnit());
						newIns.setTireId("");
						newIns.setProduceId(m.getMaterielProductionId());
					}
					erpInspectionContentMapper.insert(newIns);
					//inspections.add(newIns);
				}
				//flag = dao.saveOrUpdateAll(inspections);
			}else{
				a:
				for(ErpInspectionContent newIns : inspections){
					if(newIns.getTirePlace().equals(cta.getTireLocation())){
						newIns.setBrand(m.getMaterielBrand());
						newIns.setSpecification(m.getMaterielSpecifications());
						newIns.setSize(m.getMaterielModel());
						newIns.setProducePlace(m.getMaterielPlace());
						newIns.setState(m.getMaterielState());
						newIns.setUnit(m.getMaterielUnit());
						newIns.setTireId("");
						newIns.setProduceId(m.getMaterielProductionId());
						erpInspectionContentMapper.updateById(newIns);
						break a;
					}
				}
			}
			return flag;
		}

		//根据车辆排号获取胎位信息
		public List<String> findTirePlaces(String carPlate){
			List<String> TirePlaces = new ArrayList<String>();
			if(carPlate.equals("集中采购")){
				return null;
			}else{
				ErpCustomerCar CC = erpCustomerCarMapper.selectByPlate(carPlate);
				if(null != CC){
					String CarWheel = CC.getCarWheel();
					if(StringUtils.isNotBlank(CarWheel)){
						String[] CarWheels = CarWheel.split("-");
						for(int i = 0; i < CarWheels.length; i++){
							int CarWheelInt = Integer.parseInt(CarWheels[i]);
							if(CarWheelInt == 2){
								if(i == 0){
									TirePlaces.add("左1轮");
									TirePlaces.add("右1轮");
								}else{
									TirePlaces.add("左" + (i + 1) + "轮");
									TirePlaces.add("右" + (i + 1) + "轮");
								}
								
							}else if(CarWheelInt == 4){
								TirePlaces.add("左" + (i + 1) + "外");
								TirePlaces.add("左" + (i + 1) + "内");
								TirePlaces.add("右" + (i + 1) + "内");
								TirePlaces.add("右" + (i + 1) + "外");
							}
							
						}
					}
				}
				TirePlaces.add("备胎");
				return TirePlaces;
			}
		}
}
